package com.guigu.erp.service.impl;

import com.alibaba.druid.util.StringUtils;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import com.guigu.erp.dto.DesignFileCountDto;
import com.guigu.erp.mapper.DesignFileMapper;
import com.guigu.erp.pojo.DesignFile;
import com.guigu.erp.service.DesignFileService;
import com.guigu.erp.utils.PageUtils;
import com.guigu.erp.vo.DesignFileVo;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.List;
import java.util.Map;

@Service
public class DesignFileServiceImpl extends ServiceImpl<DesignFileMapper, DesignFile>
        implements DesignFileService {
    @Autowired
    private DesignFileMapper designFileMapper;

    @Override
    public PageUtils<DesignFile> page(DesignFileVo vo, int pageNo, int pageSize) {
        //封装查询条件
        QueryWrapper<DesignFile> queryWrapper = new QueryWrapper<>();
        //查询条件的判断
        if (vo != null) {
            if (!StringUtils.isEmpty(vo.getSearch_firstKindId())) {
                queryWrapper.eq("first_kind_id", vo.getSearch_firstKindId());
            }
            if (!StringUtils.isEmpty(vo.getSearch_secondKindId())) {
                queryWrapper.eq("second_kind_id", vo.getSearch_secondKindId());
            }
            if (!StringUtils.isEmpty(vo.getSearch_thirdKindId())) {
                queryWrapper.eq("third_kind_id", vo.getSearch_thirdKindId());
            }
            if (!StringUtils.isEmpty(vo.getSearch_type())) {
                queryWrapper.eq("type", vo.getSearch_type());
            }
            if (!StringUtils.isEmpty(vo.getSearch_registerTime1())) {
                queryWrapper.ge("register_time", vo.getSearch_registerTime1());
            }
            if (!StringUtils.isEmpty(vo.getSearch_registerTime2())) {
                queryWrapper.le("register_time", vo.getSearch_registerTime2());
            }
        }
        queryWrapper.eq("CHECK_TAG", "S001-1");
        queryWrapper.eq("DELETE_TAG", "C001-0");

        //使用分页工具
        PageHelper.startPage(pageNo, pageSize);
        List<DesignFile> designFileList = this.list(queryWrapper);
        PageInfo<DesignFile> designFilePageInfo = new PageInfo<>(designFileList);

        //将pageinfo 转换成 pageutils
        PageUtils<DesignFile> pageUtils = new PageUtils<>();
        pageUtils.setRows(designFilePageInfo.getList());
        pageUtils.setTotal(designFilePageInfo.getTotal());

        return pageUtils;
    }

    @Override
    public String getMaxFlowNumber() {
        DesignFile designFile = designFileMapper.queryMaxFile();
        // 获取产品编号
        String productId = designFile.getProductId();
        // 截取后6位流水号
        String flowNumber = productId.substring(9);
        return flowNumber;
    }

    @Override
    public int getCount() {
        return designFileMapper.queryCount();
    }

    @Override
    public PageUtils<DesignFile> getUnMakeMaterial(DesignFileVo vo, Integer pageNo, Integer pageSize) {
        //封装查询条件
        QueryWrapper<DesignFile> queryWrapper = new QueryWrapper<>();
        //查询条件的判断
        if (vo != null) {
            if (!StringUtils.isEmpty(vo.getSearch_firstKindId())) {
                queryWrapper.eq("first_kind_id", vo.getSearch_firstKindId());
            }
            if (!StringUtils.isEmpty(vo.getSearch_secondKindId())) {
                queryWrapper.eq("second_kind_id", vo.getSearch_secondKindId());
            }
            if (!StringUtils.isEmpty(vo.getSearch_thirdKindId())) {
                queryWrapper.eq("third_kind_id", vo.getSearch_thirdKindId());
            }
            if (!StringUtils.isEmpty(vo.getSearch_registerTime1())) {
                queryWrapper.ge("register_time", vo.getSearch_registerTime1());
            }
            if (!StringUtils.isEmpty(vo.getSearch_registerTime2())) {
                queryWrapper.le("register_time", vo.getSearch_registerTime2());
            }
        }

        queryWrapper.eq("CHECK_TAG", "S001-1");
        queryWrapper.eq("DESIGN_MODULE_TAG", "W001-0");
        queryWrapper.eq("DELETE_TAG", "C001-0");
        queryWrapper.eq("type", "Y001-1");

        //使用分页工具
        PageHelper.startPage(pageNo, pageSize);
        List<DesignFile> designFileList = designFileMapper.selectList(queryWrapper);
        PageInfo<DesignFile> designFilePageInfo = new PageInfo<>(designFileList);

        //将pageinfo 转换成 pageutils
        PageUtils<DesignFile> pageUtils = new PageUtils<>();
        pageUtils.setRows(designFilePageInfo.getList());
        pageUtils.setTotal(designFilePageInfo.getTotal());

        return pageUtils;
    }

    @Override
    public PageUtils<DesignFile> getMakematerialList(DesignFileVo vo, Integer pageNo, Integer pageSize) {
        //封装查询条件
        QueryWrapper<DesignFile> queryWrapper = new QueryWrapper<>();
        //查询条件的判断
        if (vo != null) {
            if (!StringUtils.isEmpty(vo.getSearch_firstKindId())) {
                queryWrapper.eq("first_kind_id", vo.getSearch_firstKindId());
            }
            if (!StringUtils.isEmpty(vo.getSearch_secondKindId())) {
                queryWrapper.eq("second_kind_id", vo.getSearch_secondKindId());
            }
            if (!StringUtils.isEmpty(vo.getSearch_thirdKindId())) {
                queryWrapper.eq("third_kind_id", vo.getSearch_thirdKindId());
            }
        }

        queryWrapper.eq("CHECK_TAG", "S001-1");
        queryWrapper.eq("DELETE_TAG", "C001-0");
        queryWrapper.eq("type", "Y001-2");

        //使用分页工具
        PageHelper.startPage(pageNo, pageSize);
        List<DesignFile> designFileList = designFileMapper.selectList(queryWrapper);
        PageInfo<DesignFile> designFilePageInfo = new PageInfo<>(designFileList);

        //将pageinfo 转换成 pageutils
        PageUtils<DesignFile> pageUtils = new PageUtils<>();
        pageUtils.setRows(designFilePageInfo.getList());
        pageUtils.setTotal(designFilePageInfo.getTotal());

        return pageUtils;
    }

    @Override
    public PageUtils<DesignFile> getUnMakeProcessList(DesignFileVo vo, Integer pageNo, Integer pageSize) {
        //封装查询条件
        QueryWrapper<DesignFile> queryWrapper = new QueryWrapper<>();
        //查询条件的判断
        if (vo != null) {
            if (!StringUtils.isEmpty(vo.getSearch_firstKindId())) {
                queryWrapper.eq("first_kind_id", vo.getSearch_firstKindId());
            }
            if (!StringUtils.isEmpty(vo.getSearch_secondKindId())) {
                queryWrapper.eq("second_kind_id", vo.getSearch_secondKindId());
            }
            if (!StringUtils.isEmpty(vo.getSearch_thirdKindId())) {
                queryWrapper.eq("third_kind_id", vo.getSearch_thirdKindId());
            }
            if (!StringUtils.isEmpty(vo.getSearch_registerTime1())) {
                queryWrapper.ge("register_time", vo.getSearch_registerTime1());
            }
            if (!StringUtils.isEmpty(vo.getSearch_registerTime2())) {
                queryWrapper.le("register_time", vo.getSearch_registerTime2());
            }
        }
        queryWrapper.eq("CHECK_TAG", "S001-1");
        queryWrapper.eq("DELETE_TAG", "C001-0");
        queryWrapper.eq("DESIGN_PROCEDURE_TAG","G001-0");
        queryWrapper.eq("TYPE","Y001-1");

        //使用分页工具
        PageHelper.startPage(pageNo, pageSize);
        List<DesignFile> designFileList = designFileMapper.selectList(queryWrapper);
        PageInfo<DesignFile> designFilePageInfo = new PageInfo<>(designFileList);

        //将pageinfo 转换成 pageutils
        PageUtils<DesignFile> pageUtils = new PageUtils<>();
        pageUtils.setRows(designFilePageInfo.getList());
        pageUtils.setTotal(designFilePageInfo.getTotal());
        return pageUtils;
    }

    @Override
    public PageUtils<DesignFile> getUnMakeSecurityConfiguration(DesignFileVo vo, Integer pageNo, Integer pageSize) {
        //封装查询条件
        QueryWrapper<DesignFile> queryWrapper = new QueryWrapper<>();
        //查询条件的判断
        if (vo != null) {
            if (!StringUtils.isEmpty(vo.getSearch_firstKindId())) {
                queryWrapper.eq("first_kind_id", vo.getSearch_firstKindId());
            }
            if (!StringUtils.isEmpty(vo.getSearch_secondKindId())) {
                queryWrapper.eq("second_kind_id", vo.getSearch_secondKindId());
            }
            if (!StringUtils.isEmpty(vo.getSearch_thirdKindId())) {
                queryWrapper.eq("third_kind_id", vo.getSearch_thirdKindId());
            }
            if (!StringUtils.isEmpty(vo.getSearch_registerTime1())) {
                queryWrapper.ge("register_time", vo.getSearch_registerTime1());
            }
            if (!StringUtils.isEmpty(vo.getSearch_registerTime2())) {
                queryWrapper.le("register_time", vo.getSearch_registerTime2());
            }
        }
        queryWrapper.eq("CHECK_TAG", "S001-1");
        queryWrapper.eq("DELETE_TAG", "C001-0");
        queryWrapper.eq("DESIGN_CELL_TAG","K001-0");

        //使用分页工具
        PageHelper.startPage(pageNo, pageSize);
        List<DesignFile> designFileList = designFileMapper.selectList(queryWrapper);
        PageInfo<DesignFile> designFilePageInfo = new PageInfo<>(designFileList);

        //将pageinfo 转换成 pageutils
        PageUtils<DesignFile> pageUtils = new PageUtils<>();
        pageUtils.setRows(designFilePageInfo.getList());
        pageUtils.setTotal(designFilePageInfo.getTotal());
        return pageUtils;
    }

    @Override
    public PageUtils<DesignFile> getSecurityConfiguration(DesignFileVo vo, Integer pageNo, Integer pageSize) {
        //封装查询条件
        QueryWrapper<DesignFile> queryWrapper = new QueryWrapper<>();
        //查询条件的判断
        if (vo != null) {
            if (!StringUtils.isEmpty(vo.getSearch_firstKindId())) {
                queryWrapper.eq("first_kind_id", vo.getSearch_firstKindId());
            }
            if (!StringUtils.isEmpty(vo.getSearch_secondKindId())) {
                queryWrapper.eq("second_kind_id",vo.getSearch_secondKindId());
            }
            if (!StringUtils.isEmpty(vo.getSearch_thirdKindId())) {
                queryWrapper.eq("third_kind_id",  vo.getSearch_thirdKindId());
            }
        }

        queryWrapper.eq("CHECK_TAG", "S001-1");
        queryWrapper.eq("DELETE_TAG", "C001-0");

        //使用分页工具
        PageHelper.startPage(pageNo, pageSize);
        List<DesignFile> designFileList = designFileMapper.selectList(queryWrapper);
        PageInfo<DesignFile> designFilePageInfo = new PageInfo<>(designFileList);

        //将pageinfo 转换成 pageutils
        PageUtils<DesignFile> pageUtils = new PageUtils<>();
        pageUtils.setRows(designFilePageInfo.getList());
        pageUtils.setTotal(designFilePageInfo.getTotal());

        return pageUtils;
    }

    @Override
    public PageUtils<DesignFile> getProductsIsCommodity(DesignFileVo vo, Integer pageNo, Integer pageSize) {
        //封装查询条件
        QueryWrapper<DesignFile> queryWrapper = new QueryWrapper<>();
        //查询条件的判断
        if (vo != null) {
            if (!StringUtils.isEmpty(vo.getSearch_firstKindId())) {
                queryWrapper.eq("first_kind_id", vo.getSearch_firstKindId());
            }
            if (!StringUtils.isEmpty(vo.getSearch_secondKindId())) {
                queryWrapper.eq("second_kind_id",vo.getSearch_secondKindId());
            }
            if (!StringUtils.isEmpty(vo.getSearch_thirdKindId())) {
                queryWrapper.eq("third_kind_id",  vo.getSearch_thirdKindId());
            }
        }

        queryWrapper.eq("type","Y001-1");
        queryWrapper.eq("CHECK_TAG", "S001-1");
        queryWrapper.eq("DELETE_TAG", "C001-0");

        //使用分页工具
        PageHelper.startPage(pageNo, pageSize);
        List<DesignFile> designFileList = designFileMapper.selectList(queryWrapper);
        PageInfo<DesignFile> designFilePageInfo = new PageInfo<>(designFileList);

        //将pageinfo 转换成 pageutils
        PageUtils<DesignFile> pageUtils = new PageUtils<>();
        pageUtils.setRows(designFilePageInfo.getList());
        pageUtils.setTotal(designFilePageInfo.getTotal());

        return pageUtils;
    }

    @Override
    public List<DesignFileCountDto> countByFactoryName() {
        return designFileMapper.countByFactoryName();
    }
}
